{extend name="layout:base" /}

{block name="content"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="test-table-reload-btn" style="margin-bottom: 10px;">
                <div class="layui-inline">
                    <button id="btnAdd" class="layui-btn layui-btn-sm layuiadmin-btn-list" data-type="add">添加内容</button>
                </div>
                <div class="layui-inline">
                    <select name="cid" id="cid" lay-filter="cid">
                        <option value="0">请选择分类</option>
                        {volist name="category_list" id="vo"}
                        <option value="{$vo.id}">{$vo.title}</option>
                        {/volist}
                    </select>
                </div>
                <div class="layui-inline">
                    <input class="layui-input" name="keywords" id="keywords" autocomplete="off">
                </div>
                <button class="layui-btn" data-type="reload">搜索</button>
            </div>


        </div>
        <div class="layui-card-body">
            <table id="tbList" class="layui-table" lay-filter="tbList" ></table>
            <script type="text/html" id="recommendTpl">
                <input type="checkbox" name="recommend" value="{{d.id}}" lay-skin="switch" lay-text="推荐|非推荐" lay-filter="recommend" {{ d.recommend== 1 ? 'checked' : '' }}>
            </script>
            <script type="text/html" id="hotTpl">
                <input type="checkbox" name="hot" value="{{d.id}}" lay-skin="switch" lay-text="热门|非热门" lay-filter="hot" {{ d.hot== 1 ? 'checked' : '' }}>
            </script>
            <script type="text/html" id="op">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>
    </div>
</div>

{/block}

{block name="script"}
<script>
    layui.config({
        base: '__STATIC__/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'util','form'], function () {

        //表格
        var $ = layui.$,form = layui.form,layer = layui.layer,table = layui.table;
        table.render({
            elem: '#tbList',
            url: "{:url('getList')}",
            cellMinWidth: 80,
            cols: [[
                {field: 'id', fixed: 'left', title: 'ID',width:80, sort: false},
                {field: 'title',fixed: 'left',title: '标题'},
                {field: 'category_name',  title: '栏目'},
                {
                    field: 'image', title: '图片',sort: true, templet: function (d) {
                        return '<image src="' + d.image + '"/>'
                    }
                },
                {field: 'sort',  title: '排序',width:80,sort: true, edit: 'text'},
                {field: 'clicks',  title: '点击率',width:90,sort: true},
                // {field: 'recommend', title: '推All',width:120,sort: true,templet: '#recommendTpl'},
                // {field: 'hot', title: '热门', templet: '#hotTpl'},
                {fixed: 'right', title: '操作',width:120, toolbar: '#op'}
            ]],
            page: true,
            limit:15
        });

        // 监听工具条
        table.on('tool(tbList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除', {icon: 3, title: '提示'}, function (index) {
                    // obj.del();
                    // layer.close(index);
                    $.post("{:url('del')}", {id: data['id']}, function (result) {
                        if (result['code'] == 0) {
                            layer.msg(result['msg'], {icon: 1, time: 2000}, function () {
                                location.reload();
                            });
                        } else {
                            layer.msg(result['msg'], {icon: 3, time: 3000});
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                var url = "{:url('edit')}?id=" + data['id'];
                var index = layer.open({
                    type: 2,
                    title: '修改内容',
                    area: ['800px', '600px'],
                    shade: 0,
                    maxmin: true,
                    content: url,
                    zIndex: layer.zIndex
                });
                layer.full(index);
            }
        });
        //监听单元格编辑
        table.on('edit(tbList)', function (obj) {
            var value = obj.value; //得到修改后的值
            var data = obj.data; //得到所在行所有键值
            var field = obj.field; //得到字段
            layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);

            $.post("{:url('edit')}", {id: data.id, sort: value}, function (result) {
                if (result['code'] == 0) {
                    layer.msg(result['msg'], {icon: 1, time: 1000}, function () {
                        location.reload();
                    });
                } else {
                    layer.msg(result['msg'], {icon: 3, time: 3000});
                }
            });
        });
        //监听推荐
        form.on('switch(recommend)', function (obj) {
            var data = {id: this.value, 'recommend': obj.elem.checked ? 1 : 0};
            $.post("{:url('edit')}", data, function (result) {
                if (result['code'] == 0) {
                    layer.msg('操作成功');
                    // location.reload();
                } else {
                    layer.msg(result['msg'], {icon: 3, time: 3000});
                }
            });
        });
        //监听推荐
        form.on('switch(hot)', function (obj) {
            var data = {id: this.value, 'hot': obj.elem.checked ? 1 : 0};
            $.post("{:url('edit')}", data, function (result) {
                if (result['code'] == 0) {
                    location.reload();
                } else {
                    layer.msg(result['msg'], {icon: 3, time: 3000});
                }
            });
        });


        //操作
        var $ = layui.$, active = {
            add: function () {
                var index = layer.open({
                    type: 2,
                    title: '添加内容',
                    area: ['800px', '600px'],
                    shade: 0,
                    maxmin: true,
                    content: "{:url('add')}",
                    zIndex: layer.zIndex
                });
                layer.full(index);
            },
            reload: function(){
                var pid=$('#pid').val();
                var cid=$('#cid').val();
                var keywords=$('#keywords').val();

                //执行重载
                table.reload('tbList', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        pid: pid
                        ,cid: cid
                        ,keywords: keywords
                    }
                });

            }
        };

        // $("#btnAdd").on('click', function () {
        //     var type = $(this).data('type');
        //     active[type] ? active[type].call(this) : '';
        // });

        $('.test-table-reload-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>
{/block}